package com.yunmang.activity.trace;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.yunmang.R;
import com.yunmang.activity.BaseActivity;
import com.yunmang.adapter.TimeAdapter;
import com.yunmang.entity.NodeBean;
import com.yunmang.entity.TraceBean;
import com.yunmang.http.HttpUtils;
import com.yunmang.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/11.
 */
public class OrderTraceActivity extends BaseActivity {
    @InjectView(R.id.toolbar)
    Toolbar mToolbar;
    @InjectView(R.id.ll_container)
    LinearLayout ll_container;
    @InjectView(R.id.lv_list)
    ListView lvList;
    @InjectView(R.id.et_hdbh_search)
    EditText etSearch;
    @InjectView(R.id.image_scan)
    ImageView ivScan;
    @InjectView(R.id.ll_search)
    LinearLayout llSearch;
    @InjectView(R.id.search)
    Button btnSearch;
    @InjectView(R.id.et_hdbh_search)
    EditText etHdbhSearch;
    private TimeAdapter adapter;
    private Subscriber<TraceBean> subScriber;
    private List<TraceBean.ModelBean> itemList;
    private List<NodeBean> nodeList;
    private ScanReceiver scanReceiver = new ScanReceiver();
    private String originallhdbh;//原始hdbh

    @Override
    public int getLayoutId() {
        return R.layout.act_track_search;
    }

    @Override
    public void initView() {
        ButterKnife.inject(this);
        initToolbar();
    }

    @Override
    public void initData() {
    }

    private void initToolbar() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("运单跟踪");
        mToolbar.setNavigationIcon(R.mipmap.ic_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void getTrackInfo(String hdbh) {
        showWaitingDialog("数据加载中……");
        subScriber = new Subscriber<TraceBean>() {
            @Override
            public void onCompleted() {
                hideWaitingDialog();
            }

            @Override
            public void onError(Throwable e) {
                hideWaitingDialog();
                e.printStackTrace();
            }

            @Override
            public void onNext(TraceBean traceBean) {
                hideWaitingDialog();
                if (traceBean.getCode() == 200) {
                    itemList = traceBean.getModel();
                    fillData();
                } else {
                }
            }
        };
        HttpUtils.getInstance().getTraceInfo(subScriber, hdbh);
    }

    private void fillData() {
        nodeList = new ArrayList<>();
        for (TraceBean.ModelBean bean : itemList) {
            NodeBean nb = new NodeBean();
            nb.notes = bean.getDescription();
            nb.actionTime = bean.getCreateTime();
            nodeList.add(nb);
        }
        adapter = new TimeAdapter(this, nodeList);
        lvList.setAdapter(adapter);
    }

    @OnClick({R.id.image_scan, R.id.search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_scan:
                CommonUtils.onScanBarcode(this);
                break;
            case R.id.search:
                CommonUtils.hideSoftInput(this);
                String hdbh = CommonUtils.getTextViewValue(etSearch);
                if (TextUtils.isEmpty(hdbh)) {
                    showToast("请输入单号");
                    return;
                }
                getTrackInfo(hdbh);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(this, "扫码取消！", Toast.LENGTH_LONG).show();
            } else {
                etSearch.setText(result.getContents());
//                Toast.makeText(this, "扫描成功，条码值: " + result.getContents(), Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    private class ScanReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("com.android.server.scannerservice.broadcast")) {
                originallhdbh = intent.getStringExtra("scannerdata").trim();
            } else if (intent.getAction().equals("android.intent.action.SCANRESULT")) {
                originallhdbh = intent.getStringExtra("value").trim();
            }
            etHdbhSearch.setText(originallhdbh.substring(0, originallhdbh.length() - 3));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.android.server.scannerservice.broadcast");
        filter.addAction("android.intent.action.SCANRESULT");
        registerReceiver(scanReceiver, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        unregisterReceiver(scanReceiver);
    }
}
